import {
  fee_record_modify,
  wallet_payout,
  parkingLot_income
} from "../../api/http"

//获取应用实例
const app = getApp()

Page({
  data: {
    showPayPwdInput: false, //是否展示密码输入层
    pwdVal: '', //输入的密码
    payFocus: false, //文本框焦点
    fee_record_id: '',
    fee_record_money: 0,
    vehicle_num: '',
    parking_record_id: ''
  },
  onLoad: function (options) {
    // 显示输入密码
    // this.showInputLayer();
    let {
      fee_record_id,
      fee_record_money,
      vehicle_num,
      parking_record_id
    } = options;
    console.log({
      fee_record_id,
      fee_record_money,
      vehicle_num,
      parking_record_id
    })
    if (fee_record_id && fee_record_money && vehicle_num && parking_record_id) {
      this.setData({
        fee_record_id,
        fee_record_money,
        vehicle_num,
        parking_record_id
      })
    }
  },
  /**
   * 显示支付密码输入层
   */
  showInputLayer: function () {
    this.setData({
      showPayPwdInput: true,
      payFocus: true
    });
  },
  /**
   * 隐藏支付密码输入层
   */
  hidePayLayer2() {
    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      pwdVal: ''
    });
  },
  async hidePayLayer() {
    var wallet_pwd = this.data.pwdVal;
    // console.log("密码:", wallet_pwd)
    // token, money, wallet_pwd
    let token = wx.getStorageSync('token')
    let money = this.data.fee_record_money
    let parking_record_id = this.data.parking_record_id
    wx.showLoading({
      title: '正在支付',
    })
    // 支付
    await wallet_payout({
        token,
        money,
        wallet_pwd
      })
      .then(
        content => {
          // console.log("支付结果:",content)
          let {
            code,
            msg
          } = content;
          if (code == 200) {
            wx.showToast({
              icon: "none",
              title: "支付成功",
            })

            return code;
          } else {
            wx.showToast({
              icon: "none",
              title: msg
            })
          }
        },
        err => {
          console.log("支付错误", err);
        }
      )
      .then(
        code => {
          if (code == 200) {
            // 支付成功 修改订单状态
            // token,parking_record_id, fee_record_status
            fee_record_modify({
                token,
                parking_record_id,
                fee_record_status: '已支付'
              })
              .then(
                content => {
                  console.log("订单状态11：", content)
                  if (content.code == 200) {
                    // 设置停车场收入
                    parkingLot_income({
                        token,
                        money
                      })
                      .then(
                        content => {
                          console.log("停车场收入：", content)
                        }
                      )

                    // 延迟跳转订单列表
                    setTimeout(() => {
                      wx.redirectTo({
                        url: '../order/order',
                      })
                    }, 500)
                  }
                }
              )
          }
        }
      )

    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      pwdVal: ''
    });

    wx.hideLoading();
  },
  /**
   * 获取焦点
   */
  getFocus: function () {
    this.setData({
      payFocus: true
    });
  },
  /**
   * 输入密码监听
   */
  inputPwd: function (e) {
    this.setData({
      pwdVal: e.detail.value
    });
    if (e.detail.value.length >= 6) {
      this.hidePayLayer();
    }



  }
})